<template>
  <div>
    <div class="search_box">
      <el-select v-model="pag.outstanding_wages">
        <el-option value="1" label="已结清"></el-option>
        <el-option value="2" label="未结清"></el-option>
      </el-select>

      <el-date-picker
        v-model="pag.time"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      >
      </el-date-picker>
      <el-button type="primary" @click="getList">搜索</el-button>
      <el-button @click="resetSearch">重置</el-button>
    </div>
    <div class="export_box">
      <el-button type="primary" @click="exportData">导出数据</el-button>
      <!-- <el-button type="primary" @click="visible = true">新增</el-button> -->
    </div>
    <el-table :data="tableData" style="width: 100%" :height="h" border>
      <el-table-column
        prop="create_time"
        label="日期"
        align="center"
        width="150px"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.start_time + "-" + scope.row.end_time }}
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="emp" label="姓名" align="center">
      </el-table-column>
      <el-table-column prop="work_day" label="出勤天数" align="center">
      </el-table-column>
      <el-table-column prop="wages1" label="基本工资" align="center">
      </el-table-column>
      <el-table-column prop="subsidies" label="生活补贴" align="center">
      </el-table-column>
      <el-table-column prop="wages" label="合计工资" align="center">
      </el-table-column>
      <el-table-column prop="social_security" label="社保扣除" align="center">
      </el-table-column>
      <el-table-column prop="tax" label="个税扣除" align="center">
      </el-table-column>
      <el-table-column prop="borrowing" label="员工借款" align="center">
      </el-table-column>
      <el-table-column prop="other" label="其他费用" align="center">
      </el-table-column>
      <el-table-column
        prop="other_remark"
        label="说明"
        align="center"
        width="250"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column prop="salary_payable" label="应发工资" align="center">
      </el-table-column>
      <el-table-column prop="paid_wages" label="已结工资" align="center">
      </el-table-column>
      <el-table-column
        prop="outstanding_wages"
        label="未结工资"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <div
            :style="{
              color: Number(scope.row.outstanding_wages || 0) > 0 ? 'red' : '',
            }"
          >
            {{ scope.row.outstanding_wages }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        prop="paid_date"
        label="实结日期"
        align="center"
        width="300"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column prop="remark" label="备注" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row)">编辑</el-button>
          <!-- <el-button type="text" @click="delData(scope.row.id)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <div class="page_table">
      <span style="line-height: 35px; margin-right: 30px"
        >应发工资:
        <span style="color: green; margin-right: 20px">{{
          Number(wages1).toFixed(4)
        }}</span>
        已结工资:
        <span style="color: green; margin-right: 20px">{{
          Number(wages2).toFixed(4)
        }}</span>
        未结工资:
        <span style="color: red">{{ Number(wages3).toFixed(4) }}</span></span
      >
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-sizes="[10, 20, 30, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pag.total"
      >
      </el-pagination>
    </div>
    <!-- 录入 -->
    <el-dialog
      :title="emp + ' - 工资表录入'"
      :visible.sync="visible"
      width="800px"
      :before-close="handleClose"
    >
      <el-form
        :inline="true"
        :model="form"
        class="demo-form-inline"
        label-width="130px"
        ref="wagesForm"
        :rules="rules"
      >
        <!-- <el-form-item label="姓名" prop="emp_id">
          <el-select v-model="form.emp_id" @change="changeEployees">
            <el-option
              v-for="item in employeesList"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="出勤日期" prop="create_time">
          <el-date-picker
            v-model="form.create_time"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy/MM/dd"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="工号" prop="job_number">
          <el-input v-model="form.job_number"></el-input>
        </el-form-item> -->
        <el-form-item label="出勤天数" prop="work_day">
          <el-input
            v-model="form.work_day"
            type="number"
            @input="changeWages"
          ></el-input>
        </el-form-item>
        <br />
        <el-form-item label="工资基础绩效" prop="wages1">
          <el-input
            v-model="form.wages1"
            type="number"
            @input="changeWages"
          ></el-input>
        </el-form-item>
        <el-form-item label="生活补贴" prop="subsidies">
          <el-input
            v-model="form.subsidies"
            type="number"
            @input="changeWages"
          ></el-input>
        </el-form-item>
        <!-- <el-form-item label="加班费" prop="overtime_price">
          <el-input
            v-model="form.overtime_price"
            style="width: 180px; margin-right: 20px"
            type="number"
            @input="changeWages"
          ></el-input>
          <el-button type="text" @click="showOther(1)">详情</el-button>
        </el-form-item>
        <el-form-item label="奖金" prop="bonus">
          <el-input
            v-model="form.bonus"
            style="width: 180px; margin-right: 20px"
            type="number"
            @input="changeWages"
          ></el-input>
          <el-button type="text" @click="showOther(2)">详情</el-button>
        </el-form-item> -->
        <el-form-item label="合计工资" prop="wages">
          <el-input v-model="form.wages" type="number"></el-input>
        </el-form-item>
        <br />
        <el-form-item label="社保扣除" prop="social_security">
          <el-input v-model="form.social_security" type="number"></el-input>
        </el-form-item>
        <el-form-item label="个税扣除" prop="tax">
          <el-input v-model="form.tax" type="number"></el-input>
        </el-form-item>

        <el-form-item label="员工借款" prop="borrowing">
          <el-input v-model="form.borrowing" type="number"></el-input>
        </el-form-item>
        <el-form-item label="其他费用" prop="other">
          <el-input v-model="form.other" type="number"></el-input>
        </el-form-item>
        <el-form-item label="其他费用说明" prop="other_remark">
          <el-input
            v-model="form.other_remark"
            type="textarea"
            :rows="2"
          ></el-input>
        </el-form-item>
        <el-form-item label="应发工资" prop="salary_payable">
          <el-input
            v-model="form.salary_payable"
            @input="changeTotal"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="已结工资" prop="paid_wages">
          <el-input
            v-model="form.paid_wages"
            @input="changeTotal"
            type="number"
          ></el-input>
        </el-form-item>
        <el-form-item label="未结工资" prop="outstanding_wages">
          <el-input v-model="form.outstanding_wages" type="number"></el-input>
        </el-form-item>
        <el-form-item label="实结日期" prop="paid_date">
          <el-input
            v-model="form.paid_date"
            type="textarea"
            :rows="3"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取 消</el-button>
        <el-button type="primary" @click="submitAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDataByKey, openDB, changeDB, deleteDB, editDB } from "@/db/indexdb";
import { searchTable } from "@/db/search";
import { printData } from "@/db/print";

export default {
  props: {
    emp: {
      type: String,
      default: "",
    },
    empid: {
      type: String,
      default: "",
    },
    toshow: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      visible: this.toshow,
      form: {
        job_number: "",
        create_time: "",
        work_day: "",
        overtime_price: "",
        bonus: "",
        wages1: "",
        subsidies: "",
        wages: "",
        borrowing: "",
        social_security: "",
        social_security_company: "",
        tax: "",
        other: "",
        salary_payable: "",
        paid_wages: "",
        outstanding_wages: "",
      },
      rules: {
        create_time: [
          { required: true, message: "请选择日期", trigger: "blur" },
        ],
        work_day: [
          { required: true, message: "请选择出勤天数", trigger: "blur" },
        ],
        wages: [{ required: true, message: "请输入合计工资", trigger: "blur" }],
        salary_payable: [
          { required: true, message: "请输入应发工资", trigger: "blur" },
        ],
      },

      tableData: [],
      employeesList: [],
      total: 0,
      wagesList1: [],
      showWagesList: [],
      showWage: false,
      showWagesType: "",
      pag: {
        emp_id: this.empid,
        outstanding_wages: "",
        time: [],
        total: 0,
        page: 1,
        size: 10,
      },
      edit: false,
      oaList: [],
      otherType: "",
      showOtherList1: [],
      showOtherList: [],
      showOtherVisible: false,
      search_other: { time: [] },
      total_price: 0.0,
      wages1: 0,
      wages2: 0,
      wages3: 0,
      totalList: [],
    };
  },
  created() {
    this.getList();
    this.getEmp();
    this.h = document.documentElement.offsetHeight - 320;
  },
  methods: {
    exportData() {
      let header = [
        { value: "emp", label: "姓名", first: 1 },
        { value: "create_time", label: "出勤日期" },
        { value: "work_day", label: "出勤天数", type: "n" },
        { value: "wages1", label: "基本工资", type: "n" },
        { value: "subsidies", label: "生活补贴", type: "n" },
        { value: "overtime_price", label: "加班费", type: "n" },
        { value: "bonus", label: "奖金", type: "n" },
        { value: "wages", label: "合计工资", type: "n", total: 1 },
        { value: "social_security", label: "社保扣除", type: "n" },
        { value: "tax", label: "个税扣除", type: "n" },
        { value: "borrowing", label: "员工借款", type: "n" },
        { value: "other", label: "其他费用", type: "n" },
        { value: "other_remark", label: "说明" },
        { value: "salary_payable", label: "应发工资", type: "n", total: 1 },
        { value: "paid_wages", label: "已结工资", type: "n", total: 1 },
        { value: "outstanding_wages", label: "未结工资", type: "n", total: 1 },
        { value: "paid_date", label: "实结日期" },
        { value: "remark", label: "备注" },
      ];
      let msg = {
        msg1: "工资表",
      };
      printData(msg, this.totalList, header);
    },
    resetSearch() {
      this.pag = {
        emp_id: this.empid,
        outstanding_wages: "",
        time: [],
        total: 0,
        page: 1,
        size: 10,
      };
      this.getList();
    },
    getList() {
      openDB("car", "list").then(() => {
        getDataByKey("list", "工资表").then((res) => {
          let result = { ...this.pag };
          result.outstanding_wages = Number(result.outstanding_wages || 0);
          result.emp_id = Number(result.emp_id || 0);
          let { total, list, totalList } = searchTable(res?.data, result, 1);
          let wages1 = 0;
          let wages2 = 0;
          let wages3 = 0;
          totalList.map((item) => {
            wages1 = Number(item.salary_payable || 0) + wages1;
            wages2 = Number(item.paid_wages || 0) + wages2;
            wages3 = Number(item.outstanding_wages || 0) + wages3;
          });
          this.totalList = totalList;
          this.wages1 = wages1;
          this.wages2 = wages2;
          this.wages3 = wages3;
          this.pag.total = total;
          this.tableData = list;
        });
      });
    },
    handleSizeChange(val) {
      this.pag.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.pag.page = val;
      this.getList();
    },
    getEmp() {
      openDB("car", "list").then(() => {
        getDataByKey("list", "员工表").then((res) => {
          this.employeesList = res.data;
          this.total = res.data.length;
        });
        getDataByKey("list", "出车记录表").then((res) => {
          this.wagesList1 = res.data;
        });
        getDataByKey("list", "OA表").then((res) => {
          this.oaList = res.data;
        });
      });
    },
    changeEployees(val) {
      let emp = this.employeesList.filter((v) => {
        return v.id == val;
      })[0];
      this.form.job_number = emp.num;
      this.form.emp = emp.name;
      this.form.overtime_price = "";
      this.form.bonus = "";
      this.changeWages();
    },
    changeWages() {
      let form = this.form;
      let total =
        (Number(form.wages1 || 0) / 30) * Number(form.work_day || 0) +
        Number(form.subsidies || 0);
      this.form.wages = Number(total).toFixed(4);
    },
    changeTotal() {
      this.form.outstanding_wages =
        Number(this.form.salary_payable || 0) -
        Number(this.form.paid_wages || 0);
    },
    handleClose() {
      this.visible = false;
      this.edit = false;
      this.form = {
        job_number: "",
        create_time: "",
        work_day: "",
        overtime_price: "",
        bonus: "",
        wages1: "",
        subsidies: "",
        wages: "",
        borrowing: "",
        social_security: "",
        social_security_company: "",
        tax: "",
        other: "",
        salary_payable: "",
        paid_wages: "",
        outstanding_wages: "",
      };
    },
    submitAdd() {
      this.$refs["wagesForm"].validate((valid) => {
        if (valid) {
          let data = { ...this.form };
          data.start_time = data.create_time[0] || "";
          data.end_time = data.create_time[1] || "";
          data.wages1 = Number(data.wages1).toFixed(4);
          data.subsidies = Number(data.subsidies).toFixed(4);
          data.wages = Number(data.wages).toFixed(4);
          data.social_security = Number(data.social_security).toFixed(4);
          data.tax = Number(data.tax).toFixed(4);
          data.borrowing = Number(data.borrowing).toFixed(4);
          data.other = Number(data.other).toFixed(4);
          data.salary_payable = Number(data.salary_payable).toFixed(4);
          data.paid_wages = Number(data.paid_wages).toFixed(4);
          data.outstanding_wages = Number(data.outstanding_wages).toFixed(4);
          data.emp = this.emp;
          data.emp_id = this.empid;
          if (this.edit) {
            editDB("list", "工资表", data).then((res) => {
              this.$message.success("修改成功");
              this.getList();
            });
          } else {
            data.id = new Date().getTime();
            changeDB("list", "工资表", data).then(() => {
              this.$message.success("添加成功");
              this.getList();
            });
          }
          this.handleClose();
        } else {
          return false;
        }
      });
    },
    editData(row) {
      console.log(row, "row");
      this.edit = true;
      this.form = { ...row };
      this.empid = row.emp_id;
      this.emp = row.emp;
      this.visible = true;
    },
    delData(id) {
      deleteDB("list", "工资表", id).then(() => {
        this.getList();
        this.visible = false;
      });
    },
    showOther(key) {
      if (this.form.emp_id == "") {
        this.$message.warning("请选择员工!");
        return;
      } else {
        this.otherType = key;
        // console.log(this.oaList, "oalist");
        let result = this.oaList.filter((item) => {
          return item.type == key && item.employees_id == this.form.emp_id;
        });
        this.showOtherList1 = result;
        this.fliterOthers();
        this.showOtherVisible = true;
      }
    },
    resetOther() {
      this.search_other.time = [];
      this.fliterOthers();
    },
    fliterOthers() {
      let { list } = searchTable(this.showOtherList1, this.search_other, 1);
      this.showOtherList = list;
    },
    handleSelectionChange(row) {
      let total_price = 0;
      if (row.length > 0) {
        row.map((item) => {
          total_price = total_price + Number(item.amount || 0);
        });
      } else {
        total_price = 0;
      }
      this.total_price = Number(total_price).toFixed(4);
    },
    addAmount() {
      if (this.otherType == 1) {
        this.form.overtime_price = this.total_price;
      } else if (this.otherType == 2) {
        this.form.bonus = this.total_price;
      }
      this.showOtherVisible = false;
      this.total_price = 0;
      this.showOtherList1 = [];
      this.showOtherList = [];
      this.search_other.time = [];
      this.otherType = "";
      this.changeWages();
    },
  },
};
</script>

<style lang="scss" scoped>
.search_box {
  display: flex;
  flex-wrap: wrap;
  border-bottom: 1px solid #ccc;
  .el-input,
  .el-select {
    width: 200px;
    margin-right: 20px;
    margin-bottom: 20px;
  }
  .el-date-editor {
    margin-right: 20px;
    margin-bottom: 20px;
  }
  .el-button {
    margin-bottom: 20px;
  }
}
.export_box {
  display: flex;
  justify-content: flex-end;
  margin: 10px 0;
}
.page_table {
  margin-top: 20px;
  width: 100%;
  display: flex;
  justify-content: flex-end;
}
.el-form {
  .el-select,
  .el-input,
  .el-date-editor {
    width: 230px;
  }
  .el-textarea {
    width: 600px;
  }
}
</style>
